Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Introduction to Array in Python
Python - Array Methods
Quiz
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - for else and while else loop

In Python, the for-else and while-else loops incorporate an else clause that is somewhat unconventional when compared to similar constructs in other programming languages. This else clause executes only after the loop finishes its normal execution—meaning it runs to completion without being interrupted by a break statement.

This feature is particularly useful for scenarios where you need to confirm that a process was completed as expected without early termination.

Python - for-else Loop

The for-else loop is useful for situations where you need to process all items in a sequence and then perform some final action if and only if the entire sequence was processed. This is commonly used in search operations where the absence of a break statement can signify that an item was not found.

Syntax

Python3
. . . .
  • for item in iterable: This initiates the loop. The loop variable item takes each value from the iterable one by one.
  • else: This keyword introduces a block that only executes if the loop exhausts the iterable without encountering a break statement.

Execution Flow

Image

Basic Example without break

Python3

. . . .

Explanation:

  • Loop Iteration: The loop iterates over each element in the numbers list and prints it.
  • Else Execution: Since there is no break statement interrupting the loop, the else clause executes right after the loop finishes, indicating that all numbers have been processed.

Example with break

Python3

. . . .

Explanation:

  • Condition Check: The loop checks each number to see if it is 3.
  • break Execution: When it finds 3, it prints a message and executes a break, stopping the loop.
  • Else Non-execution: Because the loop was interrupted by the break, the else clause does not execute.

Python - while-else Loop

The while-else loop is ideal for performing a task repeatedly under a condition and executing an action afterwards if the loop wasn't prematurely stopped by a break.

Syntax

Python3
. . . .
  • while condition: This line sets the condition for the loop to run. As long as the condition evaluates to True, the loop continues to execute.
  • else: Similar to the for-else structure, this part of the loop runs only if the while loop completes by the condition turning False and not if the loop is exited via a break statement.

Example with break

Python3

. . . .

Explanation:

  • Interrupt Condition: If count reaches 2, the loop prints a message and breaks.
  • Else Non-execution: Since the loop is interrupted by the break, the else block does not execute, signaling that the loop did not finish naturally.

These examples clearly illustrate how the else clause in Python loops adds a layer of logic that can handle both complete and interrupted loop executions. This functionality provides a powerful tool for managing loop outcomes based on whether they were able to run to completion.

Python - Loop Control
Quiz
Mark as Completed

On this page

Python - for-else Loop

Syntax

Execution Flow

Basic Example without break

Example with break

Python - while-else Loop

Syntax

Example with break